/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: queue.c
> Author:cjy
> Mail: 
> Created Time: 2024年08月09日 星期五 17时48分33秒
***************************************************/

#include "queue.h"

// 创建头节点
QUE *create_queue(){
    QUE * head = (QUE *) malloc(sizeof(QUE));
    memset(head,0,sizeof(QUE));
    head->next = NULL;
    return head;
}
// 在链表中添加数据(尾插)
/*
void push(DAT data, QUE *que) {
    QUE *point = (QUE *)malloc(sizeof(QUE));
    if (point == NULL) {
        printf("Memory allocation failed\n");
        return;
    }

    // 初始化新节点的数据
    point->data = data;
    point->next = NULL;

    // 如果队列为空，则直接添加新节点
    if (que->next == NULL) {
        que->next = point;
    } else {
        QUE *p = que;
        while (p->next != NULL) {
            p = p->next;
        }
        p->next = point;
    }

    printf("ok\n");
}
*/
void push(DAT data, QUE *que) {
    QUE *p = que;
    QUE *point = (QUE *)malloc(sizeof(QUE));
    memset(point,0,sizeof(QUE));

    if (p->next == NULL) {
        p->next = point;
        point->data = data;
        point->next = NULL;
    } else {
        while (p->next != NULL) {
            p = p->next;
        }
        p->next = point;
        point->data = data;
        point->next = NULL;
    }
    printf("ok\n");
}

// 把数据展示出来
DAT front(QUE *que){
    DAT dat;
    if (que->next == NULL) {
	memset (&dat,0,sizeof(DAT));
        return dat;
    }
    dat.type = que->next->data.type;
    strcpy(dat.count,que->next->data.count);
    dat.num1 = que->next->data.num1;
    dat.num2 = que->next->data.num2;
    strcpy(dat.ip,que->next->data.ip);
    strcpy(dat.port,que->next->data.port);
    return dat;
}
// 删除数据(头删)
void pop(QUE *que) {
    if (que->next == NULL) {
        return;
    }

    QUE *temp = que->next;
    que->next = temp->next;
    free(temp);
    temp = NULL;
}

// 遍历链表看链表中值的个数
int size(QUE *que){
    QUE *head = que->next;
    if(head == NULL) {
        return 0;
    }
    int count = 0;
    while(head != NULL) {
        count ++;
        head = head->next;
    }
    return count;
}
// 判断链表是否为空
int isempty(QUE *que){
    QUE *head = que->next;
    if(head == NULL) {
        return 0;
    }

    return -1;
}
// 释放链表
void delLinkMem(QUE *que){
    QUE *cur = NULL;
    QUE *temp = que;
    while(temp!=NULL){
        cur = temp->next;
        free(temp);
        temp = NULL;
        temp = cur;
    }
}

